package top.hidoctor.auscultation.net.p;

import android.content.Context;
import android.text.TextUtils;

import com.tang.base.BaseResponse;
import com.tang.base.MapParamsRequest;
import com.tang.base.NetMapEncryptUtils;
import com.tang.base.PatchRequest;
import com.tang.net.SuccessConsumer;
import com.tang.util.GsonUtil;

import java.io.File;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.hidoctor.auscultation.bean.DictionaryBean;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.AddXyContract;
import top.hidoctor.auscultation.net.c.UpdateContract;
import top.hidoctor.auscultation.net.req.AddLabelReq;
import top.hidoctor.auscultation.net.req.AddScreenReq;
import top.hidoctor.auscultation.net.req.DictionaryReq;

public class UpdatePresenter extends PresenterWrapper<UpdateContract.View> implements UpdateContract.Presenter {


    public UpdatePresenter(Context context, UpdateContract.View view) {
        super(context, view);
    }

    @Override
    public void update(final AddScreenReq req, String url, final int duration) {
        String path = mContext.getFilesDir().getAbsolutePath() + "/" + url;
        //上传附件
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("fileList", file.getName(), requestFile);
        final RequestBody type = RequestBody.create(MediaType.parse("multipart/form-data"), "30");
        mView.showLoading();
        add(mService.uploadFile(type, body)
                .compose(this.<String>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<String>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<String> response) {
                        req.setScreeningDetail(new AddScreenReq.ScreeningDetailBean(response.getData(), duration));
                        updateInfo(req);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();

                    }
                }));
    }

    private void updateInfo(AddScreenReq req) {
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.addScreen(new PatchRequest(maps).getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.updateSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));

    }
}
